package com.dhcc.DRGsHosp.facade.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import javax.inject.Named;

import org.openkoala.koala.commons.InvokeResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.dhcc.DRGsHosp.application.FeeStageApplication;
import com.dhcc.DRGsHosp.facade.DrgsIndexCalFacade;
import com.dhcc.DRGsHosp.facade.FeeStageFacade;
import com.dhcc.DRGsHosp.facade.dto.IndexCalDTO;
import com.dhcc.DRGsHosp.facade.impl.assembler.IndexCalAssembler;
import com.dhcc.DRGsHosp.infra.util.DrgsIndex;

/**
 * @title		:FeeStageFacadeImpl.java
 * @description	:
 * @author		:WangTeng
 * @date		:2015年11月30日
 * @version     :1.0
 * @copyright   :DHCC 2013-2014 
 */
@Named("drgsHospFacadeImpl17")
public class FeeStageFacadeImpl implements FeeStageFacade {
	
	@Autowired
	private FeeStageApplication feeStageApplication;

	@Autowired
	private DrgsIndexCalFacade drgsIndexCalFacade;
	@Override
	public InvokeResult getFeeStageData(IndexCalDTO dto) {
		dto.setDateFrom(DrgsIndex.formateDateFrom(dto.getDateFrom()));
		dto.setDateTo(DrgsIndex.formateDateTo(dto.getDateTo()));
		
		String grupScheme=drgsIndexCalFacade.getGroupScheme();
		
		List<Future<Map<String, Object>>> listFeeStage = new ArrayList<>();
		ExecutorService feeStagePool = Executors.newFixedThreadPool(2);
		
		FeeStageCallable c = new FeeStageCallable(dto,"current",grupScheme);
		Future<Map<String, Object>> currentF = feeStagePool.submit(c);
		listFeeStage.add(currentF);
		
		IndexCalDTO compareDto=new IndexCalDTO();
		BeanUtils.copyProperties(dto, compareDto); 
		String dateFromCc = DrgsIndex.getDateFromCc(dto.getDateFrom(),dto.getCompareType());
		String dateToCc = DrgsIndex.getDateFromCc(dto.getDateTo(),dto.getCompareType());
		compareDto.setDateFrom(dateFromCc);
		compareDto.setDateTo(dateToCc);
		FeeStageCallable d = new FeeStageCallable(compareDto,"compare",grupScheme);
		Future<Map<String, Object>> compareF = feeStagePool.submit(d);
		listFeeStage.add(compareF);
		
		feeStagePool.shutdown();
		//从future中取出数据
		List<Map<String, Object>> resultList=new ArrayList<>();
		List<Map<String, Object>> resultListcc=new ArrayList<>();
		for (Future<Map<String, Object>> fr : listFeeStage) {
			try {
				if("current".equals(fr.get().get("type"))){
					resultList=(List<Map<String, Object>>)fr.get().get("list");
				}
				if("compare".equals(fr.get().get("type"))){
					resultListcc=(List<Map<String, Object>>)fr.get().get("list");
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (ExecutionException e) {
				e.printStackTrace();
			}
		}
		if(resultListcc.size()>0){
			resultList = DrgsIndex.jointListMapCc(resultList, resultListcc);
		}
		return InvokeResult.success(resultList);
	}

	private class FeeStageCallable implements Callable<Map<String, Object>>{
		private IndexCalDTO dto;
		private String type;
		private String grupScheme;
		FeeStageCallable(IndexCalDTO dto,String type,String grupScheme){
			this.dto=dto;
			this.type=type;
			this.grupScheme=grupScheme;
		}
		
		@Override
		public Map<String, Object> call() throws Exception {
			Map<String, Object> result=new HashMap<String, Object>();
			List<Map<String, Object>> resultList = feeStageApplication.getFeeStageData(IndexCalAssembler.toEntity(dto),grupScheme);
			result.put("list", resultList);
			result.put("type", type);
			return result;
		}
		
	}
	@Override
	public InvokeResult getOrderData(IndexCalDTO dto) {
		dto.setDateFrom(DrgsIndex.formateDateFrom(dto.getDateFrom()));
		dto.setDateTo(DrgsIndex.formateDateTo(dto.getDateTo()));
		String grupScheme=drgsIndexCalFacade.getGroupScheme();
		List<Map<String, Object>> resultList = feeStageApplication.getOrderData(IndexCalAssembler.toEntity(dto),grupScheme);
		return InvokeResult.success(resultList);
	}

	/**
	 * 
	 *  @Description：获取医嘱开单科室特殊部门
	 *  @Author WENYI  
	 *  @see com.dhcc.DRGsHosp.facade.FeeStageFacade#getOrderSpecialDep()
	 */
	@Override
	public Object getOrderSpecialDep() {
		// TODO Auto-generated method stub
		return feeStageApplication.getOrderSpecialDep();
	}
	
	

}
